<!-- 新增/修改弹框 -->
<template>
  <!-- 新增/修改弹窗 -->
  <el-dialog
    v-model="dialogApply.visible"
    :width="900"
    :title="dialogApply.title"
    draggable
    close-icon="CloseBold"
    @close="closeDialog(false)"
  >
    <el-form
      ref="addOrEditFormRef"
      v-loading="loading"
      :model="formData"
      :rules="rules"
      label-width="100"
      size=""
      style="padding-top: 20px"
    >
      <el-row justify="start" class="d_left">
        <el-col :span="12" class="">
          <el-form-item label="选择日志" prop="code" style="margin-bottom: 18px">
            <el-input v-model="formData.code" disabled placeholder="请选择需要解锁的日志" />
            <el-button type="primary" plain style="position: absolute; right: 0px" @click="handleSelectLog"
              >选择</el-button
            >
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="项目名称">
            <el-input v-model="detailInfo.projectName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="锁定月份">
            <el-input v-model="detailInfo.lockMonth" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="锁定时间">
            <el-input v-model="detailInfo.lockTime" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="22" class="">
          <el-form-item label="锁定原因">
            <el-input v-model="detailInfo.lockReason" disabled type="textarea" :row="3" />
          </el-form-item>
        </el-col>
        <el-col :span="22" class="">
          <el-form-item label="申请复核原因" prop="unlockReason">
            <el-input
              v-model="formData.unlockReason"
              type="textarea"
              style="width: 100%"
              :row="3"
              placeholder="请说明复核意见"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="">
          <el-form-item label="申请复核资料" prop="attachments">
            <el-upload
              v-model:file-list="fileList"
              :action="actionUrl"
              multiple
              :limit="5"
              accept=".doc,.docx,.pdf,.jpg,.png,.xls,.xlsx"
              :on-exceed="handleUploadExceed"
              :on-success="handleUploadSuccess"
              :on-preview="handleUploadPreview"
            >
              <el-button plain size="small">上传附件</el-button>
              <template #tip>
                <div class="el-upload__tip">仅支持上传doc,docx,xls,xlsx,pdf,jpg,png且不大于5M的文件</div>
              </template>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <selectlog :dialog-select="dialogSelectLog" @send-close="sendClose"></selectlog>
    <template #footer>
      <div class="dialog_footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit">{{ dialogApply.footBtn }}</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ProjectLockLogEditForm } from "@/api/admin/locklog/types";
import { CreateProjectLockLog } from "@/api/admin/locklog/index";
import { FormRules } from "element-plus";
import type { UploadUserFile } from "element-plus";
import selectlog from "./components/selectlog.vue";

const props = defineProps({
  dialogApply: {
    type: Object,
    default: () => ({
      visible: false,
      title: "开户审批",
      footBtn: "确认提交",
      id: 0,
      type: 1,
    }),
  },
});

const { dialogApply } = toRefs(props);
const actionUrl = ref(`${import.meta.env.VITE_APP_BASE_API}/api/v1/common/upload`);

const emit = defineEmits(["sendClose"]);

const loading = ref(false);
const detailInfo = ref<any>({});
const formData = reactive<ProjectLockLogEditForm>({});
const addOrEditFormRef = ref(ElForm);
//上传文件列表
const fileList = ref<UploadUserFile[]>([]);

const rules = reactive<FormRules<ProjectLockLogEditForm>>({
  unlockReason: [{ required: true, message: "请填写复核意见", trigger: "blur" }],
  code: [{ required: true, message: "请选择上锁日志", trigger: "blur" }],
});

watch(
  () => dialogApply.value.visible,
  () => {
    if (dialogApply.value.visible) {
      loading.value = true;
    }
  }
);

const dialogSelectLog = reactive({
  visible: false,
  title: "选择上锁日志",
  footBtn: "确认",
});

const handleSelectLog = function () {
  dialogSelectLog.visible = true;
};
const sendClose = function (info: any) {
  dialogSelectLog.visible = false;
  if (info) {
    Object.assign(detailInfo.value, info);
    formData.code = detailInfo.value.code;
  }
};

/* 附件上传 */
const handleUploadPreview = (file: any) => {
  ElMessage.info(file.url);
};
const handleUploadExceed = () => {
  ElMessage.error("最多上传5个附件");
};
const handleUploadSuccess = (response: any) => {
  if (response.type === 0) {
    if (response.success) {
    } else {
      ElMessage.error(response.msg);
    }
  } else {
    ElMessage.error(response.msg);
  }
};

/**
 * 关闭弹窗
 */
function closeDialog(query: boolean) {
  emit("sendClose", query);
  resetForm();
}

/**
 * 重置表单
 */
function resetForm() {
  addOrEditFormRef.value.resetFields();
  addOrEditFormRef.value.clearValidate();
  formData.id = undefined;
}

/**
 * 项目表单提交
 */
function handleSubmit() {
  addOrEditFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      console.log(fileList);
      if (fileList.value.length > 0) {
        let attachments = new Array();
        fileList.value.map((item: any) => {
          if (item.status === "success") {
            if (item.response) {
              attachments.push({
                attachmentId: item.response.data,
              });
            }
          }
        });
        formData.attachments = attachments;
      }
      (formData.id = detailInfo.value.id),
        CreateProjectLockLog(formData)
          .then((res) => {
            if (res.success) {
              closeDialog(true);
            } else {
            }
          })
          .finally(() => (loading.value = false));
    }
  });
}
</script>

<style lang="scss" scoped>
.d_left {
  margin-left: 10px;
}
:deep(.el-form-item) {
  flex-wrap: wrap;
}

:deep(.el-input) {
  width: 80%;
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-input__wrapper) {
  width: 100%;
}

:deep(.el-form-item__label) {
  color: #696868;
  font-size: 13px;
}
</style>
